package com.sam.im.heiqun.uis.activities;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.sam.im.heiqun.R;
import com.sam.im.heiqun.entities.LiangSortItem;
import com.sam.im.heiqun.nets.PGService;
import com.sam.im.heiqun.uis.adapters.LiangIMAdapter;
import com.sam.im.heiqun.uis.adapters.LiangSortAdapter;
import com.sam.im.heiqun.uis.beans.GetVipNumBean;
import com.sam.im.heiqun.utils.ToolsUtils;
import com.yuyh.library.nets.callbacks.AbsAPICallback;
import com.yuyh.library.nets.exceptions.ApiException;
import com.yuyh.library.uis.activitys.BaseSwipeBackActivity;
import com.yuyh.library.utils.EventBusType;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by wangguodong on 2017/9/19.
 */

public class LiangSelecterIMActivity extends BaseSwipeBackActivity {

    @BindView(R.id.pre_v_back)
    ImageView preVBack;
    @BindView(R.id.recycler_view)
    RecyclerView recycler_view;
    @BindView(R.id.ok)
    TextView ok;

    private PGService pgservice;
    private List<GetVipNumBean.ListBean> datas = new ArrayList<>();
    private LiangIMAdapter mAdapter ;

    private String sessionid = "";
    private LiangSortItem.ListBean liangSortItem ;
    private int type = 0 ;//0-是未注册直接购买；1-已注册，购买靓号

    public static void start(Activity activity, String sessionid, LiangSortItem.ListBean liangSortItem, int type){

        Intent intent = new Intent(activity, LiangSelecterIMActivity.class);
        intent.putExtra("sessionid",sessionid);
        intent.putExtra("liangSortItem",liangSortItem);
        intent.putExtra("type",type);
        activity.startActivity(intent);

    }

    @Override
    protected String getTitleText() {
        return getResources().getString(R.string.select_im);
    }

    @Override
    protected void initViews(Bundle savedInstanceState) {
        pgservice = PGService.getInstance();
        type = getIntent().getIntExtra("type", 0);
        sessionid = getIntent().getStringExtra("sessionid");
        liangSortItem = (LiangSortItem.ListBean) getIntent().getSerializableExtra("liangSortItem");

        ok.setText(getResources().getString(R.string.update_one));
        ok.setVisibility(View.VISIBLE);

        mAdapter = new LiangIMAdapter(LiangSelecterIMActivity.this, datas);
        recycler_view.setAdapter(mAdapter);
        recycler_view.setLayoutManager(new LinearLayoutManager(this));
        mAdapter.setOnBuyClickLisener(new LiangIMAdapter.OnBuyClickLisener() {
            @Override
            public void OnBuy(int pos) {
                if (1 ==type){
                    GetVipNumBean.ListBean vipBean = datas.get(pos);

                    LiangSelecterPayTypeActivity.start(LiangSelecterIMActivity.this,1, ToolsUtils.getMyUserId(),vipBean.getPrice(),vipBean.getNum());
                }else {
                    LiangIMRegisterActivity.start(LiangSelecterIMActivity.this,sessionid,datas.get(pos));
                }

            }
        });

        inidata();
    }

    @Override
    public int getContentViewId() {
        return R.layout.activity_liang_selecter_im;
    }

    @OnClick({R.id.ok})
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()){
            case R.id.ok:
                inidata();
                break;
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN) //在ui线程执行
    public void onBaseEvent(final Integer refresh) {
        if (refresh == EventBusType.RESULT_PAY_LIANG_SUCCESS){
            finish();
        }
    }

    private void inidata() {
        showProgress(getResources().getString(R.string.now_down));
        pgservice.getVipNumber(sessionid, null==liangSortItem?"":liangSortItem.getTypeName()).subscribe(new AbsAPICallback<GetVipNumBean>() {
            @Override
            protected void onResultError(ApiException ex) {
                showToast(getResources().getString(R.string.get_data_fail));
                hideProgress();
            }

            @Override
            public void onNext(final GetVipNumBean myCircleItem) {
                if (myCircleItem != null) {
                    if (myCircleItem.getNumbers().size() > 0) {
                        datas.clear();
                        datas.addAll(myCircleItem.getNumbers());
                        mAdapter.notifyDataSetChanged();
                    }
                }
                hideProgress();
            }
        });
    }

}
